﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.WPF;
using System.Windows.Controls;
using BrainTechLLC;
using System.Windows.Data;

namespace EventWindowHandler.Plugin
{
    public class EventHandlerWindowRegisterPlugin : IRegisterMenuOption
    {
        public SimpleDispatcherTimer Timer;
        public MenuItem MenuItemShow;
        public MenuItem MenuItemTraceOnOff;
        public static PluginDefinitionLoaded<IEventInfoWindowHandler> PluginDefinition;

        public List<MenuItemAndMetadata> GetSharedMenuItems(PluginDefinitionLoadedBase pluginDefinition)
        {
            PluginDefinition = pluginDefinition as PluginDefinitionLoaded<IEventInfoWindowHandler>;
            List<MenuItemAndMetadata> results = new List<MenuItemAndMetadata>();

            MenuItemAndMetadata menuItemResult = new MenuItemAndMetadata() { TopLevelMenu = "View" };
            MenuItemShow = new MenuItem() { Header = "View _Event Info", Name = "mnuViewEventInfo", IsCheckable = true };
            MenuItemShow.Click += new System.Windows.RoutedEventHandler(menuItem_Click);
            menuItemResult.MenuItem = MenuItemShow;
            results.Add(menuItemResult);

            MenuItemAndMetadata menuItemResult2 = new MenuItemAndMetadata() { TopLevelMenu = "View" };
            MenuItemTraceOnOff = new MenuItem() { Header = "Trac_ing Events", Name = "mnuTraceOnOff", IsCheckable = true };
            MenuItemTraceOnOff.Click += new System.Windows.RoutedEventHandler(MenuItemTraceOnOff_Click);
            menuItemResult2.MenuItem = MenuItemTraceOnOff;
            results.Add(menuItemResult2);

            Timer = new SimpleDispatcherTimer(System.Windows.Threading.DispatcherPriority.Background, 300, timer =>
            {
                if (PluginDefinition.IsLoaded && PluginDefinition.Handler != null)
                {
                    Binding b1 = new Binding("IsOpen")
                    {
                        Source = PluginDefinition.Handler,
                        Mode = BindingMode.OneWay,
                    };
                    MenuItemShow.SetBinding(MenuItem.IsCheckedProperty, b1);

                    Binding b2 = new Binding("ShowDebug")
                    {
                        Source = DebugSettings.DebugSettingsInstance,
                        Mode = BindingMode.OneWay,
                    };
                    MenuItemTraceOnOff.SetBinding(MenuItem.IsCheckedProperty, b2);

                    return RunInitializationResult.StopTimer | RunInitializationResult.DidSomething;
                }

                return RunInitializationResult.DidntDoAnything;
            });

            Timer.Start();

            return results;
        }

        void MenuItemTraceOnOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (MenuItemTraceOnOff.IsChecked)
            {
                DebugSettings.DebugSettingsInstance.ShowDebug = true;
            }
            else
            {
                DebugSettings.DebugSettingsInstance.ShowDebug = false;
            }
        }

        public void ErrorReported(string errorString)
        {
            Console.WriteLine(errorString);
        }

        void menuItem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (MenuItemShow.IsChecked)
            {
                PluginDefinition.Handler.OpenEventInfoWindow(PluginDefinition.Window);
            }
            else
            {
                PluginDefinition.Handler.Close();
            }
        }
    }
}
